package com.example.demo;

import cn.hutool.core.net.URLDecoder;
import com.example.demo.service.report.ReportService;
import com.example.demo.service.task.TaskService;
import com.example.demo.service.user.UserService;
import com.example.demo.vo.ResultVO;
import com.example.demo.vo.bug.BugInfoVO;
import com.example.demo.vo.report.ReportInfoVO;
import com.example.demo.vo.user.UserVO;
import com.example.demo.vo.user.WorkerTaskVO;
import com.github.javafaker.Faker;
import org.apache.commons.codec.Charsets;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;

@SpringBootTest
public class InitData {
    @Autowired
    private DataSource dataSource;

    @Resource
    UserService userService;

    @Resource
    ReportService reportService;

    private static int userBegin=10;

    @Test
    public void init() throws IOException {
        initSql();
        Faker faker = new Faker(Locale.CHINA);
        ArrayList<String> reports=read();
        for(int i=0;i<reports.size();i++){
            //注册
            UserVO user=new UserVO();
            user.setUsername(faker.name().fullName());
            user.setPassword(faker.number().digits(10));
            user.setRole(2);
            userService.userRegister(user);
            //接任务
            WorkerTaskVO workerTaskVO=new WorkerTaskVO();
            workerTaskVO.setTakeState(1);
            workerTaskVO.setTid(2);
            workerTaskVO.setUsername(user.getUsername());
            userService.setTakeTaskState(workerTaskVO);
            //交报告
            ReportInfoVO reportInfoVO=new ReportInfoVO();
            reportInfoVO.setUsername(user.getUsername());
            reportInfoVO.setTid(2);
            ArrayList<BugInfoVO> list=new ArrayList<>();
            BugInfoVO bug=new BugInfoVO();
//            String[] info=reports.get(i).split("\\^");
            bug.setDescription(reports.get(i));
//            List<String> pics=new ArrayList<>();
//            pics.add(info[1]);
//            bug.setPictures(pics);
            bug.setProcedure("");
            bug.setDeviceInfo(1);
            list.add(bug);

            reportInfoVO.setBugList(list);
            ResultVO<ReportInfoVO> reportInfoVOResultVO = reportService.uploadReport(reportInfoVO);
            System.out.println("insert finish:"+ i+1);


        }



    }
    private ArrayList<String> read() throws IOException {
        String resource=this.getClass().getResource("/sql/ExampleReport.xlsx").getPath();
        resource= URLDecoder.decode(resource, StandardCharsets.UTF_8);
        File resourceFile=new File(resource);
        InputStream in=new FileInputStream(resourceFile);
        XSSFWorkbook sheets = new XSSFWorkbook(in);
        // 获取第一个表单Sheet
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        ArrayList<String> reports=new ArrayList<>();
        for (int i = 1; i < 76; i++) {
            XSSFRow row = sheetAt.getRow(i);
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            // 读取每一格内容
            XSSFCell xssfCell=row.getCell(1);
//            XSSFCell picture=row.getCell(2);
            xssfCell.setCellType(CellType.STRING);
//            picture.setCellType(CellType.STRING);
            reports.add(xssfCell.getStringCellValue());
        }
        return reports;

    }

    private void initSql(){
        //通过数据源获取数据库链接
        Connection connection = DataSourceUtils.getConnection(dataSource);
        //创建脚本执行器
        ScriptRunner scriptRunner = new ScriptRunner(connection);
        //创建字符输出流，用于记录SQL执行日志
        StringWriter writer = new StringWriter();
        PrintWriter print = new PrintWriter(writer);
        //设置读取文件格式
        Resources.setCharset(Charsets.UTF_8);
        String[] filePaths = {"sql\\init.sql"};
        for (String path : filePaths) {

            Reader reader = null;
            try {
                //获取资源文件的字符输入流
                reader = Resources.getResourceAsReader(path);
            } catch (IOException e) {
                //文件流获取失败，关闭链接
                scriptRunner.closeConnection();
                return;
            }
            //执行SQL脚本
            scriptRunner.runScript(reader);
            //关闭文件输入流
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        //关闭输入流
        scriptRunner.closeConnection();
    }
}
